package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.service.impl.SetmealServiceImpl;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "套餐相关接口")
public class SetmealController {

    @Autowired
    private SetmealServiceImpl setmealService;
    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<PageResult> getPage(SetmealPageQueryDTO setmealPageQueryDTO)
    {
        log.info("获取套餐分页接口数据:{}",setmealPageQueryDTO);
        PageResult pageResult=setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    //新增菜品
    @ApiOperation("新增菜品接口")
    @PostMapping()
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result addSetmeal(@RequestBody SetmealDTO setmealDTO)
    {
        log.info("接收到新增套餐数据:{}",setmealDTO);
        //Long categoryId = setmealDTO.getCategoryId();
        //String key="setmeal_"+categoryId;
        //cleanCache(key);
        setmealService.addSetmeal(setmealDTO);
        return Result.success();
    }

    //套餐的起售和停售
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    @ApiOperation("套餐起售和停售")
    @PostMapping("/status/{status}")
    public Result SetmealStatus(@PathVariable Integer status ,Long id)
    {
        cleanCache("setmeal_*");
        setmealService.setStatusById(status,id);
        return Result.success();
    }

    //修改套餐
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    @PutMapping()
    @ApiOperation("修改套餐接口")
    public Result updateSetmeal(@RequestBody SetmealDTO setmealDTO)
    {
        // cleanCache("setmeal_*");
        setmealService.updateSetmeal(setmealDTO);
        return Result.success();
    }


    //根据id查询套餐用于页面回显
    @ApiOperation("根据套餐id查询套餐")
    @GetMapping("/{id}")
    public Result<SetmealVO> getSetmealById(@PathVariable Long id)
    {
        log.info("接收到套餐id:{}",id);
        SetmealVO SetmealVO=setmealService.getSetmealById(id);
        log.info("接收到响应数据:{}",SetmealVO);
        return Result.success(SetmealVO);
    }


    //删除套餐
    @DeleteMapping()
    @ApiOperation("删除套餐")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result deleteSetmeal(@RequestParam List<Long> ids)
    {
        log.info("接收到需要删除的id集合{}",ids);
        //cleanCache("setmeal_*");
        setmealService.deleteSetmeal(ids);
        return Result.success();
    }

    //清除套餐缓存
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);//获取key值
        //删除
        redisTemplate.delete(keys);
    }
}
